﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Prologis.ShifterNT.FileHandler
{
    public class FileMoveHandler : IFileMoveHandler
    {

        string importedSubdir;
        string errorSubdir;
        string errorFormatString;
        string importedFormatString;


        /// <summary>
        /// Initializes a new instance of the <see cref="FileMoveHandler"/> class.
        /// </summary>
        /// <param name="importedSubdir">The imported subdir.</param>
        /// <param name="errorSubdir">The error subdir.</param>
        /// <param name="errorFormatString">The error format string.</param>
        /// <param name="importedFormatString">The imported format string.</param>
        public FileMoveHandler(string importedSubdir, string errorSubdir, string importedFormatString, string errorFormatString)
        {
            this.importedSubdir = importedSubdir;
            this.errorSubdir = errorSubdir;
            this.errorFormatString = errorFormatString;
            this.importedFormatString = importedFormatString;
        }


        /// <summary>
        /// Converts the filename.
        /// </summary>
        /// <param name="file">Name of the file.</param>
        /// <param name="formatString">The format string.</param>
        /// <returns></returns>
        public static string ConvertFilename(string file, string formatString)
        {
            var fileName = Path.GetFileName(file);
            var dateTimeString = DateTime.Now.ToString(formatString);
            var formattedFilename = String.Format(dateTimeString, fileName);
            return formattedFilename;
        }


        /// <summary>
        /// Gets the move target path.
        /// </summary>
        /// <param name="currentFilePath">The imported file path.</param>
        /// <param name="targetSubdir">The move target subdir.</param>
        /// <param name="formatString">The format string.</param>
        /// <returns></returns>
        public static string GetTargetPath(string currentFilePath, string targetSubdir, string formatString)
        {
            var currentDirectory = Path.GetDirectoryName(currentFilePath);
            var targetDirectory = Path.Combine(currentDirectory, targetSubdir);
            var convertedFilename = FileMoveHandler.ConvertFilename(currentFilePath, formatString);
            var targetPath = Path.Combine(targetDirectory, convertedFilename);

            return targetPath;
        }


        public static void EnsureDirectoryExists(string directory)
        {


        }

        public static void MoveFile(string sourceFilePath, string targetFilePath)
        {
            string targetDirectory = Path.GetDirectoryName(targetFilePath);
            if (false == Directory.Exists(targetDirectory))
            {
                Directory.CreateDirectory(targetDirectory);
            }

            File.Move(sourceFilePath, targetFilePath);

            
        }





        /// <summary>
        /// Runs the specified file path.
        /// </summary>
        /// <param name="filePath">The file path.</param>
        /// <param name="type">The type.</param>
        /// <returns></returns>
        public string Run(string filePath, FileMoveType type)
        {

            string targetPath = "";

            switch (type)
            {
                case FileMoveType.ToImported:
                    {
                        targetPath = FileMoveHandler.GetTargetPath(filePath, this.importedSubdir, this.importedFormatString);
                        FileMoveHandler.MoveFile(filePath, targetPath);
                    }
                    break;

                case FileMoveType.ToError:
                    {
                        targetPath = FileMoveHandler.GetTargetPath(filePath, this.importedSubdir, this.errorFormatString);
                        FileMoveHandler.MoveFile(filePath, targetPath);
                    }
                    break;

                default:
                    throw new NotImplementedException();
                   
            }


            return targetPath;
        

        }


    }
}
